
export default G6 => {
    G6.registerBehavior('select-node', {
        getDefaultCfg() {
            return {
                multiple: false // 多选
            }
        },
        getEvents() {
            return {
                'node:click': 'onNodeClick',
                'node:dblclick': 'onNodeDblClick',
                'node:contextmenu': 'onNodeContextmenu'
            }
        },
        onNodeClick(e) {
            const graph = this.graph;
            const item = e.item;
            this._clearSelected();
            graph.setItemState(item, 'nodeState', 'selected');
            graph.emit('after-node-selected', e);
        },
        onNodeDblClick(e) {
            const graph = this.graph;
            const item = e.item;
            this._clearSelected();
            // item.toFront(); // 提高层级
            graph.setItemState(item, 'nodeState', 'selected');
            graph.emit('after-node-dblclick', e);
        },
        onNodeContextmenu(e) {
            this.graph.emit('on-editor-contextmenu', {
                type: 'node',
                event: e
            })
        }, // 清除选中的节点和边样式
        _clearSelected() {
            let graph = this.graph;
            const selectNodes = this.graph.findAllByState('node', 'nodeState:selected');
            selectNodes.forEach(node => {
                graph.setItemState(node, 'nodeState', 'default');
            })
            const selectEdges = this.graph.findAllByState('edge', 'edgeState:selected');
            selectEdges.forEach(edge => {
                graph.setItemState(edge, 'edgeState', 'default');
            })
        }
    })
}